﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Xrm.Sdk.Metadata;

using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Indentation;

namespace VisualRibbonEditor
{
	public partial class ViewXmlDialog : UserControl, IDialog
	{
		public ViewXmlDialog()
		{
			InitializeComponent();

			XmlBox.Text = MainWindow.CurrentSolution.RibbonDiffXml.ToString();

			XmlFoldingStrategy foldingStrategy = new XmlFoldingStrategy();
			XmlBox.TextArea.IndentationStrategy = new DefaultIndentationStrategy();
			FoldingManager foldingManager = FoldingManager.Install(XmlBox.TextArea);
			foldingStrategy.UpdateFoldings(foldingManager, XmlBox.Document); 
		}
		
		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			if (DialogClosed != null) DialogClosed(null);
		}
		 
		public event CloseDialogHandler DialogClosed;

		private void ViewXmlDialog_KeyUp(object sender, KeyEventArgs e)
		{
			switch (e.Key) //
			{
				// ESC
				case Key.Escape:
					if (DialogClosed != null) DialogClosed(null);
					break;
				default:
					break;
			}
		}

		private void WordWrap_OnChange(object sender, RoutedEventArgs e)
		{
			XmlBox.WordWrap = (WordWrap.IsChecked != null && WordWrap.IsChecked.Value == true);
		}

		private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
		{
			MenuItem_Copy.IsEnabled = !String.IsNullOrEmpty(XmlBox.SelectedText);
		}
	}
}
